package com.jeeplus.juhe;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class RNAUtil {
	public static RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
	
	// 配置您申请的KEY
	public static final String APPKEY = "d6671a052287ce424fa51b2e188bb064";
	public static String query_url = "http://op.juhe.cn/idcard/query?key=" + APPKEY;
	public static String verifys_url = "http://op.juhe.cn/idcard/verifys?key=" + APPKEY;
	public static String queryEncry_url = "http://op.juhe.cn/idcard/queryEncry?key=" + APPKEY;
	
	public static final String OPENID = "JH2425507785ce2328fad73e30020a1e69";
	
	
	/**
	 * 请求接口查询数据
	 * @param params 参数
	 * @param type 类型，1明文查询，2验签版，3加密版
	 * @return
	 * @throws Exception
	 */
	public static String queryResult(Map<String, Object> params, int type) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String result = null;
		String url = query_url;
		switch (type) {
		case 2:
			url = verifys_url;
			break;
		case 3:
			url = queryEncry_url;
			break;
		}
		try {
			url = new StringBuffer(url).append("&").append(urlencode(params)).toString();
			System.out.println(url);
			HttpGet httpget = new HttpGet(url);
			httpget.setConfig(config);
			response = httpClient.execute(httpget);
			HttpEntity resEntity = response.getEntity();
			if (resEntity != null) {
				result = IOUtils.toString(resEntity.getContent(), "UTF-8");
			}
			EntityUtils.consume(resEntity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			response.close();
			httpClient.close();
		}
		return result;
	}
	
	
	
	// 将map型转为请求参数型
	public static String urlencode(Map<String, ?> data) {
		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, ?> i : data.entrySet()) {
			try {
				sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		String result = sb.toString();
		result = result.substring(0, result.lastIndexOf("&"));
		return result;
	}

	
	
	/**
	 * 把字符串转换成md5
	 * @param input
	 * @return
	 */
	public static String MD5(String data) {
		StringBuffer md5str = new StringBuffer();
		byte[] input = data.getBytes();
		try {
			// 创建一个提供信息摘要算法的对象，初始化为md5算法对象
			MessageDigest md = MessageDigest.getInstance("MD5");
			// 计算后获得字节数组
			byte[] buff = md.digest(input);
			// 把数组每一字节换成16进制连成md5字符串
			// 把数组每一字节换成16进制连成md5字符串
			int digital;
			for (int i = 0; i < buff.length; i++) {
				digital = buff[i];
				if (digital < 0) {
					digital += 256;
				}
				if (digital < 16) {
					md5str.append("0");
				}
				md5str.append(Integer.toHexString(digital));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return md5str.toString();
	}

}
